/**
 * @file weather.c
 *
 */

/*********************
*      INCLUDES
*********************/
#include "lv_watch.h"

#if USE_LV_WATCH_WEATHER != 0

#include <stdio.h>

/*********************
*      DEFINES
*********************/

/**********************
*      TYPEDEFS
**********************/

/**********************
*  STATIC PROTOTYPES
**********************/
static lv_obj_t * weather_content_no_info_create(lv_obj_t * par);
static lv_obj_t * weather_content_create(lv_obj_t * par, app_adaptor_weather_t * weather);
static lv_obj_t * temperature_content_create(lv_obj_t * par, app_adaptor_weather_t * weather);
static lv_obj_t * detail_content_create(lv_obj_t * par, app_adaptor_weather_t * weather);
static void weather_update(lv_anim_t * anim);
static void weather_update_anim_start(lv_obj_t * cont);
/**********************
*  STATIC VARIABLES
**********************/
const watch_weather_list weather_list[] = {
    { ICON_SUNNY,          WATCH_TEXT_ID_SUNNY                               }, /* WATCH_WEATHER_ID_SUNNY = 0,   */
    { ICON_CLOUDY,         WATCH_TEXT_ID_CLOUDY                              }, /* WATCH_WEATHER_ID_CLOUDY,      */
    { ICON_LIGHT_RAIN,     WATCH_TEXT_ID_LIGHT_RAIN                          }, /* WATCH_WEATHER_ID_LIGHT_RAIN,  */
    { ICON_HEAVY_RAIN,     WATCH_TEXT_ID_HEAVY_RAIN                          }, /* WATCH_WEATHER_ID_HEAVY_RAIN,  */
    { ICON_LIGHT_SNOW,     WATCH_TEXT_ID_LIGHT_SNOW                          }, /* WATCH_WEATHER_ID_LITTLE_SNOW, */
    { ICON_HEAVY_SNOW,     WATCH_TEXT_ID_HEAVY_SNOW                          }, /* WATCH_WEATHER_ID_HEAVY_SNOW,  */
    { ICON_FOG,            WATCH_TEXT_ID_FOG                                 }, /* WATCH_WEATHER_ID_FOG,         */
    { ICON_HAZE,           WATCH_TEXT_ID_HAZE                                }, /* WATCH_WEATHER_ID_HAZE,        */
    { ICON_WEATHER_OTHERS, WATCH_TEXT_ID_OTHER_WEATHER                       }, /* WATCH_WEATHER_ID_OTHERS = 99, */
    { ICON_NO_NETWORK,     WATCH_TEXT_ID_NO_NETWORK                          }, /* WATCH_WEATHER_ID_NO_INFO = 255*/
};

/**********************
*      MACROS
**********************/

/**********************
 *   GLOBAL FUNCTIONS
 ***********************/
lv_obj_t * weather_create(lv_obj_t * tileview, uint16_t id)
{
    lv_obj_t * tab;
    if(id == 0xFF) {
        tab = tileview;
    } else {
        tab = launcher_tileview_add_tile(tileview, id);

        LV_ASSERT_MEM(tab);
        if(tab == NULL) {
            return(NULL);
        }
    }

    // containt for weather
    lv_obj_t * weather_cont = lv_cont_create(tab, NULL);
    lv_weather_ext_t * weather_ext = lv_obj_allocate_ext_attr(weather_cont, sizeof(lv_weather_ext_t));
    lv_obj_set_click(weather_cont, false);
    lv_obj_set_size(weather_cont, lv_obj_get_width(tab), lv_obj_get_height(tab));
    lv_obj_add_style(weather_cont, LV_CONT_PART_MAIN, &lv_watch_style_transp);
    weather_ext->content1 = NULL;
    weather_ext->content2 = NULL;
    weather_ext->content3 = NULL;

    weather_update_anim_start(weather_cont);

    return(tab);
}

void weather_update(lv_anim_t * anim)
{
    app_adaptor_weather_t weather_info = {};
    app_adaptor_get_weather_req(&weather_info);

    lv_watch_png_cache_all_free();

    lv_obj_t * cont = (lv_obj_t *)anim->var;
    lv_weather_ext_t * weather_ext = (lv_weather_ext_t *)lv_obj_get_ext_attr(cont);
    if(weather_ext->content1 != NULL) {
        lv_obj_del(weather_ext->content1);
        weather_ext->content1 = NULL;
    }

    if(weather_ext->content2 != NULL) {
        lv_obj_del(weather_ext->content2);
        weather_ext->content2 = NULL;
    }

    if(weather_ext->content3 != NULL) {
        lv_obj_del(weather_ext->content3);
        weather_ext->content3 = NULL;
    }

    if(WATCH_WEATHER_ID_NO_INFO == weather_info.weather_id) {
        weather_ext->content1 = weather_content_no_info_create(cont);
        lv_obj_align(weather_ext->content1, cont, LV_ALIGN_IN_TOP_MID, 0, 20);
    } else {
        weather_ext->content1 = weather_content_create(cont, &weather_info);
        lv_obj_align(weather_ext->content1, cont, LV_ALIGN_IN_TOP_MID, 0, 20);

        weather_ext->content2 = temperature_content_create(cont, &weather_info);
        lv_obj_align(weather_ext->content2, weather_ext->content1, LV_ALIGN_OUT_BOTTOM_LEFT, 20, 10);

        weather_ext->content3 = detail_content_create(cont, &weather_info);
        lv_obj_align(weather_ext->content3, weather_ext->content1, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 10);
    }
}

/**********************
*   STATIC FUNCTIONS
**********************/
static void weather_update_anim_start(lv_obj_t * cont)
{
    lv_anim_t a;

    lv_anim_init(&a);
    lv_anim_set_var(&a, cont);
    lv_anim_set_time(&a, 1);
    lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE);
    lv_anim_set_repeat_delay(&a, 5000);       // update every 1s
    lv_anim_set_start_cb(&a, weather_update);
    lv_anim_start(&a);
}

static lv_obj_t * weather_content_no_info_create(lv_obj_t * par)
{
    lv_obj_t * content = lv_watch_cont_create(par, LV_LAYOUT_COLUMN_MID, &lv_style_transp_fit, lv_obj_get_width(par));

    lv_obj_t * img = lv_img_create(content, NULL);

    LV_ASSERT_MEM(img);
    if(img == NULL) {
        return(NULL);
    }
    lv_img_set_src(img, weather_list[9].img_src);

    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font30);
    lv_label_set_text_id(label, weather_list[9].txtId);
    lv_obj_set_click(label, false);

    return(content);
}

static lv_obj_t * weather_content_create(lv_obj_t * par, app_adaptor_weather_t * weather)
{
    lv_obj_t * content = lv_watch_cont_create(par, LV_LAYOUT_COLUMN_MID, &lv_style_transp_fit, lv_obj_get_width(par));
    lv_obj_t * img     = lv_img_create(content, NULL);
    int8_t   index;

    if(weather->weather_id >= sizeof(weather_list) / sizeof(weather_list[0]) - 1) {
        index = 8;
    } else {
        index = weather->weather_id;
    }

    LV_ASSERT_MEM(img);
    if(img == NULL) {
        return(NULL);
    }
    lv_img_set_src(img, weather_list[index].img_src);

    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);

    lv_label_set_text_id(label, weather_list[index].txtId);

    lv_obj_set_click(label, false);

    return(content);
}

static lv_obj_t * temperature_content_create(lv_obj_t * par, app_adaptor_weather_t * weather)
{
    char     str[10] = "";

    lv_obj_t * content = lv_watch_cont_create(par, LV_LAYOUT_ROW_TOP, &lv_watch_style_transp, lv_obj_get_width(par) / 2);
    lv_obj_set_base_dir(content, LV_BIDI_DIR_LTR);

    lv_obj_t * label = lv_label_create(content, NULL);

    lv_obj_set_style_local_pad_inner(content, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font40_yellow);
    snprintf(str, 10, "%d", weather->current_temperature);
    lv_label_set_text(label, str);
    lv_obj_set_click(label, false);

    label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font10);
    lv_label_set_text(label, "o");
    lv_obj_set_click(label, false);
    lv_obj_set_style_local_pad_top(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, -2);

    label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font40_yellow);
    lv_label_set_text(label, "C");
    lv_obj_set_click(label, false);

    return(content);
}

static lv_obj_t * detail_content_create(lv_obj_t * par, app_adaptor_weather_t * weather)
{
    int8_t   lowest_level  = weather->lowest_temperature;
    int8_t   highest_level = weather->highest_temperature;
    char     str[10]       = {};
    lv_obj_t * content      = lv_watch_cont_create(par, LV_LAYOUT_COLUMN_LEFT, &lv_watch_style_transp, lv_obj_get_width(
                                  par) / 2);
    lv_obj_set_base_dir(content, LV_BIDI_DIR_LTR);
    lv_cont_set_layout(content, LV_LAYOUT_OFF);
    lv_obj_t * label = lv_label_create(content, NULL);

    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    if(NULL != weather->city) {
        lv_label_set_text(label, weather->city);
    } else {
        lv_label_set_text(label, "");
    }

    lv_obj_set_click(label, false);

    lv_obj_t * content1 = lv_watch_cont_create(content, LV_LAYOUT_ROW_TOP, &lv_watch_style_transp, lv_obj_get_width(
                              par) / 2);

    lv_obj_align(content1, label, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 0);
    lv_obj_set_style_local_pad_inner(content1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0);
    label = lv_label_create(content1, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    snprintf(str, 10, "%d-%d", lowest_level, highest_level);
    lv_label_set_text(label, str);
    lv_obj_set_click(label, false);


    label = lv_label_create(content1, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font10);
    lv_obj_set_style_local_pad_top(label, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, -2);
    lv_label_set_text(label, "o");
    lv_obj_set_click(label, false);

    label = lv_label_create(content1, NULL);
    lv_obj_add_style(label, LV_LABEL_PART_MAIN, &lv_watch_font20);
    lv_label_set_text(label, "C");
    lv_obj_set_click(label, false);

    return(content);
} /* detail_content_create */

#endif /*USE_LV_WATCH_WEATHER*/
